package cn.iocoder.yudao.module.social.api.interaction;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.social.api.interaction.dto.InteractionReqDTO;
import cn.iocoder.yudao.module.social.api.interaction.dto.InteractionRespDTO;
import cn.iocoder.yudao.module.social.convert.interaction.InteractionConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.interaction.InteractionDO;
import cn.iocoder.yudao.module.social.service.interaction.InteractionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.social.enums.ErrorCodeConstants.INTERACTION_NOT_EXISTS;

/**
 * 互动 API 实现类
 *
 * @author 芋道源码
 */
@RestController
@Validated
@Slf4j
public class InteractionApiImpl implements InteractionApi {

    @Resource
    private InteractionService interactionService;

    @Override
    public CommonResult<Long> createInteraction(InteractionReqDTO reqDTO) {
        try {
            Long id = interactionService.createInteraction(InteractionConvert.INSTANCE.convertToCreateReqVO(reqDTO));
            return success(id);
        } catch (Exception ex) {
            log.error("[createInteraction][reqDTO({}) 创建互动异常]", reqDTO, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<Boolean> deleteInteraction(Long id) {
        try {
            interactionService.deleteInteraction(id);
            return success(true);
        } catch (Exception ex) {
            log.error("[deleteInteraction][id({}) 删除互动异常]", id, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<InteractionRespDTO> getInteraction(Long id) {
        try {
            InteractionDO interaction = interactionService.getInteraction(id);
            if (interaction == null) {
                return CommonResult.error(INTERACTION_NOT_EXISTS);
            }
            return success(InteractionConvert.INSTANCE.convertApi(interaction));
        } catch (Exception ex) {
            log.error("[getInteraction][id({}) 查询互动异常]", id, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<InteractionRespDTO>> getInteractionList(Collection<Long> ids) {
        try {
            List<InteractionDO> interactions = interactionService.getInteractionList(ids);
            return success(InteractionConvert.INSTANCE.convertApiList(interactions));
        } catch (Exception ex) {
            log.error("[getInteractionList][ids({}) 批量查询互动异常]", ids, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<InteractionRespDTO>> getInteractionListByContent(Long contentId) {
        try {
            List<InteractionDO> interactions = interactionService.getInteractionListByContent(contentId);
            return success(InteractionConvert.INSTANCE.convertApiList(interactions));
        } catch (Exception ex) {
            log.error("[getInteractionListByContent][contentId({}) 根据内容查询互动异常]", contentId, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<InteractionRespDTO>> getInteractionListByUser(Long userId) {
        try {
            List<InteractionDO> interactions = interactionService.getInteractionListByUser(userId);
            return success(InteractionConvert.INSTANCE.convertApiList(interactions));
        } catch (Exception ex) {
            log.error("[getInteractionListByUser][userId({}) 根据用户查询互动异常]", userId, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<InteractionRespDTO>> getInteractionListByType(String type) {
        try {
            List<InteractionDO> interactions = interactionService.getInteractionListByType(type);
            return success(InteractionConvert.INSTANCE.convertApiList(interactions));
        } catch (Exception ex) {
            log.error("[getInteractionListByType][type({}) 根据类型查询互动异常]", type, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

}
